/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package argentracker.activeentity.registry;

import argentracker.activeentity.ActiveEntity;

import com.google.appengine.api.datastore.Entity;

/**
 * Registry of ae metadata.
 * 
 * @author matero (matero _at_ gmail _dot_ com)
 */
public final class ActiveEntityRegistry {

    /**
     * ActiveEntityRegistry to be used, it can not be <code>null</code>.
     */
    private static ActiveEntityRegistryStrategy aeRegistry = new DefaultActiveEntityRegistryStrategy();

    /**
     * Avoids the possibility of Registry explicit construction.
     */
    private ActiveEntityRegistry() {
        throw new UnsupportedOperationException("this class should not be instantiated");
    }
    
    /**
     * Install an ae registry, setting it as the one to be used as backend of the class.
     * 
     * @param newRegistry
     *            ae registry to use.
     * @throws IllegalArgumentException ifnewRegistry is <code>null</code>.
     */
    public static final synchronized void install(ActiveEntityRegistryStrategy newRegistry) {
        if (newRegistry == null) {
            throw new IllegalArgumentException("newRegistry can not be null");
        }
        aeRegistry = newRegistry;
    }
    
    public static boolean isActiveEntityDefinedOn(Class<?> clazz) {
        return aeRegistry.isActiveEntityDefinedOn(clazz);
    }

    public static <AE extends ActiveEntity<AE>> AE createActiveEntityFrom(Entity e, Class<AE> aeClass) {
        return aeRegistry.createActiveEntityFrom(e, aeClass);
    }
}
